<div class="panel panel-default panel-intro">
    <div class="panel-heading">
        <h3 class="panel-title">合并同名物料</h3>
        <div class="panel-actions">
            <a href="{:url('index')}" class="btn btn-default">返回物料列表</a>
        </div>
    </div>
    <div class="panel-body">
        {if condition="empty($duplicates)"}
        <div class="alert alert-success">
            <h4><i class="icon fa fa-check"></i> 没有重复物料</h4>
            <p>系统中没有发现同名物料，无需合并。</p>
        </div>
        {else}
        <div class="alert alert-warning">
            <h4><i class="icon fa fa-warning"></i> 发现重复物料</h4>
            <p>系统中发现以下同名物料，建议合并以避免数据混乱。</p>
        </div>
        
        <div class="row">
            <div class="col-md-12">
                <div class="box">
                    <div class="box-header with-border">
                        <h3 class="box-title">重复物料列表</h3>
                    </div>
                    <div class="box-body">
                        <table class="table table-bordered table-striped">
                            <thead>
                                <tr>
                                    <th width="5%">#</th>
                                    <th width="30%">物料名称</th>
                                    <th width="20%">物料ID</th>
                                    <th width="20%">库存数量</th>
                                    <th width="15%">重复数量</th>
                                    <th width="10%">合并后库存</th>
                                </tr>
                            </thead>
                            <tbody>
                                {foreach name="duplicates" item="vo" key="k"}
                                <tr>
                                    <td>{$k+1}</td>
                                    <td><strong>{$vo.name}</strong></td>
                                    <td>
                                        {php}
                                            $ids = explode(',', $vo['ids']);
                                            $stocks = explode(',', $vo['stocks']);
                                            foreach($ids as $i => $id) {
                                                echo '<span class="label label-info" style="margin-right:5px;">ID:' . $id . '</span>';
                                            }
                                        {/php}
                                    </td>
                                    <td>
                                        {php}
                                            $ids = explode(',', $vo['ids']);
                                            $stocks = explode(',', $vo['stocks']);
                                            foreach($stocks as $i => $stock) {
                                                echo '<span class="text-primary" style="margin-right:10px;">' . $stock . '</span>';
                                            }
                                        {/php}
                                    </td>
                                    <td>
                                        <span class="label label-warning">{$vo.count}个</span>
                                    </td>
                                    <td>
                                        <strong class="text-success">
                                            {php}
                                                $stocks = explode(',', $vo['stocks']);
                                                echo array_sum($stocks);
                                            {/php}
                                        </strong>
                                    </td>
                                </tr>
                                {/foreach}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="row">
            <div class="col-md-12">
                <div class="alert alert-info">
                    <h4><i class="icon fa fa-info"></i> 合并说明</h4>
                    <ul>
                        <li>合并后，将保留第一个物料记录，删除其他重复记录</li>
                        <li>所有重复物料的库存将合并到保留的物料中</li>
                        <li>所有相关表（工序物料配置、订单物料、采购申请等）的物料ID将自动更新</li>
                        <li>此操作不可逆，请谨慎操作</li>
                    </ul>
                </div>
            </div>
        </div>
        
        <div class="row">
            <div class="col-md-12 text-center">
                <form method="post" action="">
                    <button type="submit" class="btn btn-danger btn-lg" onclick="return confirm('确定要合并所有同名物料吗？此操作不可逆！');">
                        <i class="fa fa-compress"></i> 合并所有同名物料
                    </button>
                </form>
            </div>
        </div>
        {/if}
    </div>
</div>
